package com.bank.common.io;

import  com.bank.common.exception.ErrorCode;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

/**
 * @author Fu Zhaohui
 */
@Setter
@Getter
public class RestVo<T> {

    private int code = 200;
    private boolean success = true;
    private String msg;

    @ApiModelProperty("返回结果")
    private T result;

    public RestVo() {
    }

    public RestVo(T result) {
        this.result = result;
    }

    public RestVo(Integer code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public static <T> RestVo<T> ok(){
        RestVo<T> result = new RestVo<>();
        return result;
    }

    public static <T> RestVo<T> ok(T t){
        RestVo<T> result = new RestVo<>();
        result.setResult(t);
        return result;
    }

    public static <T> RestVo<T> error(){
        RestVo<T> result = new RestVo<>();
        result.setCode(ErrorCode.ERROR.getErrCode());
        result.setMsg(ErrorCode.ERROR.getErrMsg());
        return result;
    }

    public static <T> RestVo<T> error(String msg){
        RestVo<T> result = new RestVo<>();
        result.setCode(ErrorCode.ERROR.getErrCode());
        result.setMsg(msg);
        return result;
    }

    public static <T> RestVo<T> error(int code,String msg){
        RestVo<T> result = new RestVo<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }
    public void setAuthException(String msg) {
        this.code = 401;
        this.msg = msg;
        this.success = false;
    }

    public void setSecureException(String msg) {
        this.code = 403;
        this.msg = msg;
        this.success = false;
    }

    public void setCommonException(String msg) {
        this.code = 400;
        this.msg = msg;
        this.success = false;
    }

    public void setGrpcException(String msg) {
        this.code = 501;
        this.msg = msg;
        this.success = false;
    }

    public void setPayException(String msg) {
        this.code = 505;
        this.msg = msg;
        this.success = false;
    }

    public void setNotFoundException(String msg) {
        this.code = 404;
        this.msg = msg;
        this.success = false;
    }

    public void setServerException(String msg) {
        this.code = 500;
        this.msg = msg;
        this.success = false;
    }

}
